package com.tql.mall.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tql.mall.auth.mapper.AdminMapper;
import com.tql.mall.auth.mapper.CustomerMapper;
import com.tql.mall.auth.mapper.MerchantMapper;
import com.tql.mall.auth.service.UserService;
import com.tql.mall.model.customer.Customer;
import com.tql.mall.model.merchant.Merchant;
import com.tql.mall.model.user.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @Author: tyk
 * @Date: 2023/5/24 19:32
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public Customer loginCustomer(Customer customer) {
        // 密码加密
        String pwd = DigestUtils.md5DigestAsHex(customer.getPasswd().getBytes());
        // 查询条件
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getPasswd, pwd).eq(Customer::getAccount, customer.getAccount());
        // 返回结果
        return customerMapper.selectOne(wrapper);
    }

    @Override
    public Merchant loginMerchant(Merchant merchant) {
        // 密码加密
        String pwd = DigestUtils.md5DigestAsHex(merchant.getPasswd().getBytes());
        // 查询条件
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getPasswd, pwd).eq(Merchant::getAccount, merchant.getAccount());
        // 返回结果
        return merchantMapper.selectOne(wrapper);
    }

    @Override
    public Admin loginAdmin(Admin admin) {
        // 密码加密
        String pwd = DigestUtils.md5DigestAsHex(admin.getPasswd().getBytes());
        // 查询条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getPasswd, pwd).eq(Admin::getAccount, admin.getAccount());
        // 返回结果
        return adminMapper.selectOne(wrapper);
    }
}
